package symptomsSearch;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import ctm.javacode.dbBean;

import javax.naming.NamingException;

/*
 * 檔名 : KeywordAttributeClassification.java
 * 功能 : 關鍵字之屬性查詢、分類以及關鍵字標準化

 *******************************************************************
 * 函式 :QueryKeywordAttribute()											
 * 功能 : 關鍵字標準化 及 關鍵字之屬性查詢											
 * 參數 :SplitSymptoms 為 斷詞後原始症狀字串。例 :小便-大便-痛 
 * 產生  標準化關鍵字與關鍵字之屬性 對應表 StandardKeywordAndAttributeMapping
 * 		標準化關鍵字			屬性
 * 		       大便			   object
 * 		       小便			   object
 * 		       疼痛			    Value
 ********************************************************************

 *******************************************************************
 * 函式 :KeywordAttributeClassification()
 * 功能 :關鍵字之屬性分類
 * 參數 :SplitSymptoms 為 斷詞後原始症狀字串。例 :小便-大便-痛 
 *******************************************************************
 *******************************************************************
 * 函式 :GetObjectList()
 * 參數 :
 * 回傳 :
 *******************************************************************

 ********************************************************************
 * 函式 :GetValueList()
 * 參數 :
 * 回傳 :
 * *******************************************************************

 */
public class KeywordAttributeClassification {

	private LinkedList<String> standard_keyword_not_exist = new LinkedList<String>();
	private Map<String, String> StandardKeywordAndAttributeMapping = new LinkedHashMap<String, String>();
	private LinkedList<String> object = new LinkedList<String>();
	private LinkedList<String> attribute_value = new LinkedList<String>();

	private void QueryKeywordAttribute(String SplitSymptoms)
			throws NamingException, SQLException {
		/*
		 * 連結資料庫設定
		 */
		dbBean db = new dbBean("jdbc/ctmpllab");
		db.Connected();

		String[] SplitSymptomsList;
		SplitSymptomsList = SplitSymptoms.split("-");

		for (int SplitSymptomsList_index = 0; SplitSymptomsList_index < SplitSymptomsList.length; SplitSymptomsList_index++) {

			String sqlQuery = "Select * from synonym_table Left join standard_table on FIND_IN_SET( standard_table.name , synonym_table.standard_keyword) where synonym_table.keyword = '"
					+ SplitSymptomsList[SplitSymptomsList_index] + "'";
			ResultSet rs;
			rs = db.ExcuteQuery(sqlQuery);

			if (!rs.next()) {

				standard_keyword_not_exist
						.add(SplitSymptomsList[SplitSymptomsList_index]);

			} else {

				do {

					StandardKeywordAndAttributeMapping.put(
							rs.getString("standard_table.name"),
							rs.getString("standard_table.class"));

				} while (rs.next());

			}

		}
		db.CloseConn();
	}

	public void KeywordAttributeClassification(String SplitSymptoms)
			throws NamingException, SQLException {

		QueryKeywordAttribute(SplitSymptoms);

		Object o = null;
		Set set = StandardKeywordAndAttributeMapping.entrySet();
		Iterator it = set.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			if (entry.getValue().equals("object")) {
				o = entry.getKey();
				object.add(o.toString());

			}

			else if (entry.getValue().equals("value")) {
				o = entry.getKey();
				attribute_value.add(o.toString());

			} else {

				o = entry.getKey();
				object.add(o.toString());
				attribute_value.add(o.toString());
			}
		}

	}

	public LinkedList<String> GetObjectList() {

		return object;

	}

	public LinkedList<String> GetValueList() {

		return attribute_value;

	}

	public LinkedList<String> GetNoExistStandardKeywordList() {

		return standard_keyword_not_exist;

	}

}
